home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c3 / pro13 / p.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-21  |  2.1 KB  |  89 lines

  1. /*
  2.  * Sample program of ways for computing with numbers larger than normal
  3.  * written in Turbo C.  This program computes powers of '2'. 'N' is the
  4.  * variable holds the power to which the number is being raised (i.e. 2^N)
  5.  */
  6. #include <time.h>
  7. #include <dos.h>
  8.  
  9.   typedef int          number[10000];
  10.  
  11.  number       arr;
  12.  int          x, i, place, N, tens, ones;
  13.  char         bell;
  14.  struct time  start, end;
  15.  
  16.  
  17. void         Roundoff()
  18. {
  19.   for (i = 0; i <= place - 1; i++)
  20.   {
  21.     while (((arr[i] / tens) > 0))
  22.     {
  23.       arr[i + ones] = arr[i + ones] + ((arr[i] / tens) % 10);
  24.       ones = ones + 1;
  25.       tens = tens * 10;
  26.     }
  27.     arr[i] = arr[i] % 10;
  28.     ones = 1;
  29.     tens = 10;
  30.   }
  31. }
  32.  
  33. void         Multiply()
  34. {
  35.   for (i = 0; i <= place; i++)
  36.   arr[i] = arr[i] * 2;
  37. /*  Change the '2' in previous line to compute the power of a different number.
  38.  */
  39. }
  40.  
  41. void         Setplace()
  42. {
  43.   if ((arr[place - 1] != 0) && (place < 10000)) place = place + (x * arr[place - 1] / 10);
  44. }
  45.  
  46.  
  47. main(argc,argv)
  48. int argc;
  49. char *argv[];
  50. { if (argc > 1)
  51.     N = atoi(argv[1]);
  52.   else
  53.   { printf("\n");
  54.   printf("Program to Calculate 2^N for up to 10000 digits in final answer\n");
  55.   printf("\n");
  56.   printf("Please enter the power of 2 to calculate\n");
  57.   printf("N = ? ");
  58.   scanf("%d",&N); }
  59.   for (i = 0; i <= 10000; i++) arr[i] = 0;
  60.   place = 4;
  61.   tens = 10;
  62.   ones = 1;
  63.   bell = '\007';
  64.   printf("%c\n",bell);
  65.   gettime(&start);
  66.   arr[0] = 1;
  67.   for (x = 1; x <= N; x++)
  68.   {
  69.     Multiply();
  70.     Roundoff();
  71.     Setplace();
  72.   }
  73.   gettime(&end);
  74.   printf("Start time was  %02d:%02d:%02d:%02d\n", start.ti_hour,
  75.      start.ti_min, start.ti_sec, start.ti_hund);
  76.   printf("Finish time was %02d:%02d:%02d:%02d\n", end.ti_hour,
  77.      end.ti_min, end.ti_sec, end.ti_hund);
  78.   printf("2^%d =  \n",N);
  79.   while (arr[place] == 0) place = place - 1;
  80.   for (i = 0; i <= place; i++)
  81.   {
  82.     printf("%d",arr[place - i]);
  83.     if (((place - i) % 3 == 0) && (i < place)) printf(",");
  84.     if ((place - i) % 54 == 0) printf("\n");
  85.   }
  86.   printf("\n");
  87.   printf("%d digits%c\n",place + 1,bell);
  88. }
  89.